package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.bjesc_b2c.app.appraiset.widget.popupwindow.BasicDropDownPop;
import com.rxretrofit_build.CarPhotoID;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.CarBigPhotoStatusBean;
import rxretrofit_base.model.KeyBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by lenovo on 2017/2/28.
 * 内饰
 */

public class CarOrnamentsItemActivity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.tv_car_ceiling)
    TextView carCeilingTv;
    @InjectView(R.id.tv_car_carpet)
    TextView carCarpetTv;
    @InjectView(R.id.tv_car_dashboard)
    TextView carDashboardTv;
    @InjectView(R.id.tv_car_steering_wheel)
    TextView carSteeringWheelTv;
    @InjectView(R.id.tv_left_front_door)
    TextView leftFrontDoorTv;
    @InjectView(R.id.tv_right_front_door)
    TextView rightFrontDoorTv;
    @InjectView(R.id.tv_right_rear_door)
    TextView rightRearDoorTv;
    @InjectView(R.id.tv_left_front_seat)
    TextView leftFrontSeatTv;
    @InjectView(R.id.tv_right_front_seat)
    TextView rightFrontSeatTv;
    @InjectView(R.id.tv_console)
    TextView consoleTv;
    @InjectView(R.id.tv_rear_seat)
    TextView rearSeatTv;
    @InjectView(R.id.tv_left_rear_door)
    TextView leftRearDoorTv;
    @InjectView(R.id.tv_boot)
    TextView bootTv;
    @InjectView(R.id.btn_previous_page)
    Button previousPageBtn;
    @InjectView(R.id.btn_next_page)
    Button nextPage;

    private BasicDropDownPop basicPop;
    private final int startRequest=88;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_ornaments);
        ButterKnife.inject(this);
        basicPop = new BasicDropDownPop(this,this);
        getOrnamentsStatus();
    }

    @OnClick({R.id.iv_return, R.id.tv_save_info, R.id.tv_car_ceiling, R.id.tv_car_carpet, R.id.tv_car_dashboard,
            R.id.tv_car_steering_wheel, R.id.tv_left_front_door, R.id.tv_right_front_door, R.id.tv_right_rear_door,
            R.id.tv_left_front_seat, R.id.tv_right_front_seat, R.id.tv_console, R.id.tv_rear_seat, R.id.tv_left_rear_door,
            R.id.tv_boot, R.id.btn_previous_page, R.id.btn_next_page,R.id.rlty_title})
    public void onClick(View view) {
        Intent intent = new Intent(CarOrnamentsItemActivity.this, CarCeilingActivity.class);
        switch (view.getId()) {
            case R.id.iv_return:        //返回
                finish();
                break;
            case R.id.tv_save_info:     //保存    //已经取消
                break;
            case R.id.tv_car_ceiling:   //顶棚
                buildIntent(intent, CarPhotoID.CarOrnamentsID.ceiling, "顶棚");
                startIntent(intent, startRequest);
                break;
            case R.id.tv_car_carpet:    //地毯
                buildIntent(intent, CarPhotoID.CarOrnamentsID.carpet, "地毯");
                startIntent(intent, startRequest);
                break;
            case R.id.tv_car_dashboard: //方向盘
                buildIntent(intent, CarPhotoID.CarOrnamentsID.dashboard, "方向盘");
                startIntent(intent, startRequest);
                break;
            case R.id.tv_car_steering_wheel:     //仪表盘
                buildIntent(intent, CarPhotoID.CarOrnamentsID.steeringWheel, "仪表盘");
                startIntent(intent, startRequest);
                break;
            case R.id.tv_left_front_door:       //左前门
                buildIntent(intent, CarPhotoID.CarOrnamentsID.leftFrontDoor, "左前门");
                startIntent(intent, startRequest);
                break;
            case R.id.tv_right_front_door:      //右前门
                buildIntent(intent, CarPhotoID.CarOrnamentsID.rightFrontDoor, "右前门");
                startIntent(intent, startRequest);
                break;
            case R.id.tv_right_rear_door:       //右后门
                buildIntent(intent, CarPhotoID.CarOrnamentsID.rightRearDoor, "右后门");
                startIntent(intent, startRequest);
                break;
            case R.id.tv_left_front_seat:       //左前座
                buildIntent(intent, CarPhotoID.CarOrnamentsID.leftFrontSeat, "左前座");
                startIntent(intent, startRequest);
                break;
            case R.id.tv_right_front_seat:      //右前座
                buildIntent(intent, CarPhotoID.CarOrnamentsID.rightFrontSeat, "右前座");
                startIntent(intent, startRequest);
                break;
            case R.id.tv_console:               //中控台
                buildIntent(intent, CarPhotoID.CarOrnamentsID.console, "中控台");
                startIntent(intent, startRequest);
                break;
            case R.id.tv_rear_seat:             //后座
                buildIntent(intent, CarPhotoID.CarOrnamentsID.rearSeat, "后座");
                startIntent(intent, startRequest);
                break;
            case R.id.tv_left_rear_door:        //左后门
                buildIntent(intent, CarPhotoID.CarOrnamentsID.leftRearDoor, "左后门");
                startIntent(intent, startRequest);
                break;
            case R.id.tv_boot:                  //后备箱
                buildIntent(intent, CarPhotoID.CarOrnamentsID.boot, "后备箱");
                startIntent(intent, startRequest);
                break;
            case R.id.btn_previous_page:        //上一页外观

                startIntent(new Intent(CarOrnamentsItemActivity.this, CarSurfaceItemActivity.class), true);
                break;
            case R.id.btn_next_page:            //下一页装置
                startIntent(new Intent(CarOrnamentsItemActivity.this, DeviceActivity.class), true);
                break;
            case R.id.rlty_car_form:        //车源图片
                startIntent(new Intent(CarOrnamentsItemActivity.this, CarSourceActivity.class),true);
                break;
            case R.id.rlty_car_Framework:   //骨架
                startIntent(new Intent(CarOrnamentsItemActivity.this, CarFrameworkImageActivity.class),true);
                break;
            case R.id.rlty_car_appearance:  //外观
                startIntent(new Intent(CarOrnamentsItemActivity.this,CarSurfaceItemActivity.class),true);
                break;
            case R.id.rlty_car_interior:    //内饰
             basicPop.dismiss();
                break;
            case R.id.rlty_car_damage:      //配置
                startIntent(new Intent(CarOrnamentsItemActivity.this, DeviceActivity.class),true);
                break;
            case R.id.rlty_car_comprehensive:   //综合评估
                startIntent(new Intent(CarOrnamentsItemActivity.this, ComprehensiveEvaluationActivity.class),true);
                break;
            case R.id.rlty_car_photo:       //合影
                startIntent(new Intent(CarOrnamentsItemActivity.this, PhotographsActivity.class),true);
                break;
            case R.id.rlty_title:
                basicPop.showAsDropDown(findViewById(R.id.rlty_title));
                break;
        }
    }

    private void getOrnamentsStatus() {
        HashMap<String, Object> build = new MapUtil()
                .put("id",  CustomSharedPreferences.getString(this, KeyBean.CARID.valueOf(),KeyBean.FILE_NAME.valueOf()))
                .put("class_id", CarPhotoID.ornamentsID)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(this, build).create(IApiActivityRequest.class).getFrameworkStatus(),
                new BaseSubscriber<TokenBean<List<CarBigPhotoStatusBean>>>(this) {
                    @Override
                    public void onSuccess(TokenBean<List<CarBigPhotoStatusBean>> o) {
                        if (o.getData() != null && o.getData().size() > 0) {
                            for (int i = 0; i < o.getData().size(); i++) {
                                judgmentStatus(o.getData().get(i).getAttr_id(), o.getData().get(i).getColor());
                            }

                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }

    /**
     * 跳转页面需要带的参数
     *
     * @param intent
     * @param sourceID 图片ID
     */
    private void buildIntent(Intent intent, String sourceID, String title) {
        intent.putExtra("sourceID", sourceID);
        intent.putExtra("classID", CarPhotoID.ornamentsID);
        intent.putExtra("title", title);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode ==startRequest ){
            if(data!=null){
                judgmentStatus(data.getStringExtra("sourceID"), data.getStringExtra("color") );
            }
        }
    }

    /**
     * 判断哪些控件需要改变状态
     *
     * @param status 需要改变的ID
     */
    private void judgmentStatus(String status, String level) {
        if(TextUtils.isEmpty(status)){
            return;
        }
        switch (status) {
            case CarPhotoID.CarOrnamentsID.ceiling:   //顶棚
                setViewStatus(level,  carCeilingTv);
                break;
            case CarPhotoID.CarOrnamentsID.carpet:    //地毯
                setViewStatus(level, carCarpetTv);
                break;
            case CarPhotoID.CarOrnamentsID.dashboard: //方向盘
                setViewStatus(level ,carDashboardTv);
                break;
            case CarPhotoID.CarOrnamentsID.steeringWheel:     //仪表盘
                setViewStatus(level, carSteeringWheelTv);
                break;
            case CarPhotoID.CarOrnamentsID.leftFrontDoor:       //左前门
                setViewStatus(level, leftFrontDoorTv);
                break;
            case CarPhotoID.CarOrnamentsID.rightFrontDoor:      //右前门
                setViewStatus(level,  rightFrontDoorTv);
                break;
            case CarPhotoID.CarOrnamentsID.rightRearDoor:       //右后门
                setViewStatus(level, rightRearDoorTv);
                break;
            case CarPhotoID.CarOrnamentsID.leftFrontSeat:       //左前座
                setViewStatus(level,leftFrontSeatTv);
                break;
            case CarPhotoID.CarOrnamentsID.rightFrontSeat:      //右前座
                setViewStatus(level,rightFrontSeatTv);
                break;
            case CarPhotoID.CarOrnamentsID.console:               //中控台
                setViewStatus(level, consoleTv);
                break;
            case CarPhotoID.CarOrnamentsID.rearSeat:             //后座
                setViewStatus(level,rearSeatTv);
                break;
            case CarPhotoID.CarOrnamentsID.leftRearDoor:        //左后门
                setViewStatus(level,  leftRearDoorTv);
                break;
            case CarPhotoID.CarOrnamentsID.boot:                  //后备箱
                setViewStatus(level, bootTv);
                break;
        }
    }

    /**
     * 设置控件状态
     *
     * @param view
     */
    private void setViewStatus(String level, TextView view) {
        if(TextUtils.isEmpty(level)){
            return;
        }
        if ("green".equals(level)) {
            changeGreenFrame(view);
        } else if ("yellow".equals(level)) {

            changeYellowFrame(view);
        } else if ("orange".equals(level)) {

            changeOrangeFrame(view);
        } else if ("red".equals(level)) {
            changeRadFrame(view);
        } else {
//            changeWhiteFrame(view); //取消状态
            changeGreenFrame(view);
        }
    }


    /**
     * 设置1级状态状态
     *
     * @param v
     */
    private void changeGreenFrame(TextView v) {
        v.setTextColor(getResources().getColor(R.color.colorWhite));
        v.setBackgroundResource(R.color.colorFF33B16A);
    }

    /**
     * 设置2级状态状态
     *
     * @param v
     */
    private void changeYellowFrame(TextView v) {
        v.setTextColor(getResources().getColor(R.color.colorWhite));
        v.setBackgroundResource(R.color.colorFFF5A823);
    }

    /**
     * 设置3级状态状态
     *
     * @param v
     */
    private void changeOrangeFrame(TextView v) {
        v.setTextColor(getResources().getColor(R.color.colorWhite));
        v.setBackgroundResource(R.color.colorFFFF7900);
    }

    /**
     * 设置4级状态状态
     *
     * @param v
     */
    private void changeRadFrame(TextView v) {
        v.setTextColor(getResources().getColor(R.color.colorWhite));
        v.setBackgroundResource(R.color.colorFFF7041B);
    }

    /**
     * 取消状态
     *
     * @param v
     */
    private void changeWhiteFrame(TextView v) {
        v.setTextColor(getResources().getColor(R.color.colorFF030303));
        v.setBackgroundResource(R.drawable.shape_text_frame_bbbbbb);
    }
}
